package cn.sc.summer.mybatis.strategy.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.sc.summer.mybatis.enums.DataSourceTypeEnum;
import cn.sc.summer.constant.mybatisplus.datasource.TableColumn;
import cn.sc.summer.constant.mybatisplus.datasource.TableColumnParam;
import cn.sc.summer.constant.mybatisplus.datasource.TableInfo;
import cn.sc.summer.constant.mybatisplus.datasource.TableInfoParam;
import cn.sc.summer.constant.util.StrUtilX;
import cn.sc.summer.mybatis.strategy.basestage.TableInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类名：mysql的表信息获取
 *
 * @author a-xin
 * @date 2024/8/27 14:17
 */
@Slf4j
@Service
public class MysqlTableInfoService extends TableInfoService {

    private static final long serialVersionUID = -3350622895963584819L;

    /**
     * 获取指定数据源中表格的数据信息
     *
     * @param jdbcTemplate 连接对象
     * @return 数据库表字段信息
     */
    @Override
    public List<TableInfo> getColumnInfo(JdbcTemplate jdbcTemplate) {

        List<TableInfoParam> tables = super.getTableInfo(jdbcTemplate);
        if (CollUtil.isEmpty(tables)) {
            return new ArrayList<>();
        }

        Map<String, String> tableComment = tables.stream()
                .collect(Collectors.toMap(TableInfoParam::getTableName, TableInfoParam::getTableComment, (v1, v2) -> v2));
        List<Map<String, Object>> tableDetailMaps = super.queryColumnInfo(jdbcTemplate);
        List<TableColumnParam> tableDetail = super.getTableDetail(tableDetailMaps);
        Map<String, List<TableColumnParam>> tableAndColumnMap = tableDetail.stream()
                .collect(Collectors.groupingBy(TableColumnParam::getTableName));

        List<TableInfo> tableInfos = new ArrayList<>();
        tableAndColumnMap.forEach((tableName, columns) -> {

            TableInfo tableInfo = new TableInfo();
            tableInfo.setTableName(tableName);
            tableInfo.setTableComment(tableComment.get(tableName));
            tableInfo.setTableSchema(tables.get(0).getTableSchema());

            List<TableColumn> tableColumns = columns.stream().map(column -> {
                TableColumn tableColumn = new TableColumn();
                tableColumn.setTableName(tableName);
                tableColumn.setColumnName(column.getColumnName());
                tableColumn.setColumnType(column.getColumnType());
                tableColumn.setColumnComment(column.getColumnComment());
                tableColumn.setColumnDefault(column.getColumnDefault());
                tableColumn.setDataType(column.getDataType());
                tableColumn.setMaxLength(column.getMaxLength());
                tableColumn.setNullAble(StrUtilX.strToBool(column.getNullAble()));
                return tableColumn;
            }).collect(Collectors.toList());
            tableInfo.setTableColumns(tableColumns);
            tableInfos.add(tableInfo);
        });

        return tableInfos;
    }

    /**
     * 确定该类支持的数据源类型
     *
     * @return 支持的数据源类型
     */
    public String supports() {
        return DataSourceTypeEnum.MYSQL.getType();
    }

}
